<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>排放照片列表页面</title>
    <link rel="stylesheet" type="text/css" href="../../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../../css/style.css" />
    <link rel="stylesheet" type="text/css" href="../../css/weui.min.css">
    <script type="text/javascript" src="../../script/jquery-1.10.2.min.js"></script>
</head>
<body> 
    <div class="container" id="container">
        <div id="divLXArea" class="weui_cells weui_cells_form" style="margin-top:0px">
            <div class="mainbutton">
                <a class="weui_btn weui_btn_primary"  href="javascript:;" id="btnWGStart" onclick="WJStartLX()">排放开始</a> 
            </div>
            <br>
            <div class="mainbutton">
                <a class="weui_btn weui_btn_primary"  href="javascript:;" id="btnWGXM" onclick="OpenWJXM()">排放外检项目</a> 
            </div>
            <div class="weui_cell">
                <div class="weui_cell_hd"><label class="weui_label">检测线代号:</label></div>
                    <div class="weui_cell_bd weui_cell_primary">
                        <select class="select" style="width: 150px" id="selectLineNum">
                            <option selected="selected" value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                            <option value="5">5</option>
                            <option value="6">6</option>
                            <option value="7">7</option>
                            <option value="8">8</option>
                            <option value="9">9</option>
                            <option value="10">10</option>
                            <option value="11">11</option>
                            <option value="12">12</option>
                            <option value="13">13</option>
                            <option value="14">14</option>
                            <option value="15">15</option>
                        </select>
                    </div>
            </div>
            <div id="divKSSJ" class="weui_cells_title">检验时间:
                <sapn id="spKSSJInfo" style="color:red;">未开始</sapn>
            </div>  
            <div id="divListHeader" class="weui_cells_title">
                <sapn id="spVehicleInfo"></sapn>
            </div>  
        </div>
    </div> 
</body>
</html>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript">
    var UIListView;
    var dialogBox;
    apiready = function(){
        $("#spVehicleInfo").html(api.pageParam.PlateNum + ", " + api.pageParam.LWLSH);
        UIListView = api.require('UIListView');
        photoBrowser = api.require('photoBrowser');
        dialogBox = api.require('dialogBox');
        GetZPItemsByID(api.pageParam.LWLSH);

        GetLXKSSJ(api.pageParam.ID);

        var vLineNum = $api.getStorage('LineNum');
        if(vLineNum != null)
        {
            $("#selectLineNum").val(vLineNum);
        }
    };

    function InitZPDHList(data)
    {
        if(data.length != 0)
        {
            var vData = new Array([data.length]);
            for (i = 0; i < data.length; i++) {
                vData[i] = {
                    text: data[i].ZPMC,
                    zpdh: data[i].ZPDH
                }
            }

            return vData;
        }
        else
        {
            return null;
        }

    }

    function InsetZPItemsToList(vData)
    {
        var vIndex = UIListView.getCount(function(retIndex) {
                var vIndexZP = retIndex.count;
                UIListView.insertItem({
                index: vIndexZP,
                data: vData,

                }, function(ret, err) {
                if (ret) {
                    //UpdateZPBtn(vIndexZP);
                    //GetZPItemsByID();
                } 
            });
        });      
    }

    function GetLXKSSJ(paraID)
    {
        var vUrlRead = $api.getStorage('ServerUrlRead');
        api.ajax({
            url:vUrlRead + '/WcfServices/GetWJStartTime_PF/'+paraID,
            method: 'post',
            contentType: "application/json",
        }, function(ret, err) {
            if (ret.IsSuccess) {
                $("#spKSSJInfo").html(ret.Message);
                if(ret.Message != "未开始")
                {
                    $("#btnWGXM").removeClass("weui_btn_default");
                    $("#btnWGXM").addClass("weui_btn_primary");  
                }
            }
        });
    }

    function InitZPItem(vDataInfo)
    {
        var  vData = {
                uid: vDataInfo.ID,
                title: vDataInfo.ZPMC,
                imgPath:'widget://res/img/Camera.png',
                remark: vDataInfo.ZPZT_MS
            };
        return vData;  
    }

    function WJStartLX()
    {
        var vClass = $("#btnWGStart").attr("class");
        var array=vClass.split(" "); 

        var vEnable = false;
        for(var i = 0;i<array.length;i++)
        {
            if(array[i] == "weui_btn_primary")
            {
                vEnable = true;
                break;
            }
        }

        if(vEnable)
        {
            $("#btnWGStart").removeClass("weui_btn_primary");
            $("#btnWGStart").addClass("weui_btn_default");

             var vUrlWrite = $api.getStorage('ServerUrlWrite');
             var vUserCHName = $api.getStorage('UserCHName');
             var vUserSFZH = $api.getStorage('UserSFZH');
             var vJCXDH = $("#selectLineNum").val();
             $api.setStorage('LineNum', vJCXDH);
             api.ajax({
                url:vUrlWrite + '/WcfServices/WJStart_PF/'+api.pageParam.ID + '/'+ vUserCHName+ '/'+ vUserSFZH +'/'+vJCXDH,
                method: 'post',
                contentType: "application/json",
            }, function(ret, err) {
                if (ret.IsSuccess) {
                    $("#spKSSJInfo").html(ret.Data);

                    $("#btnWGXM").removeClass("weui_btn_default");
                    $("#btnWGXM").addClass("weui_btn_primary");

                    $("#btnWGStart").removeClass("weui_btn_primary");
                    $("#btnWGStart").addClass("weui_btn_default");
                }
                else
                {
                    api.alert({
                        title: '提示',
                        msg: ret.Message,
                    }, function(ret, err){

                    });

                    $("#btnWGStart").removeClass("weui_btn_default");
                    $("#btnWGStart").addClass("weui_btn_primary");
                }
            });
        }
    }

    function OpenWJXM()
    {
        var vClass = $("#btnWGXM").attr("class");
        var array=vClass.split(" "); 

        var vEnable = false;
        for(var i = 0;i<array.length;i++)
        {
            if(array[i] == "weui_btn_primary")
            {
                vEnable = true;
                break;
            }
        }

        if(vEnable)
        {
            $api.fixStatusBar( $api.dom('header'));
            var vHeightLX = $("#divLXArea").height();
            var vHeightKSSJ = $("#divKSSJ").height();
            var vHeightListHeader = $("#divListHeader").height();
            var vHeight = vHeightLX + vHeightKSSJ + vHeightListHeader +25;


            api.openFrame({
            name: 'frm_pfwgjyxm',
            url: './frm_pfwgjyxm.html',
            rect: {
                x: 0, 
                y: vHeight, 
                w: api.winWidth, 
                h: 'auto'
            },
            bounces : false,
            pageParam: {
                ID: api.pageParam.ID,
                PlateNum: api.pageParam.PlateNum,
                LWLSH: api.pageParam.LWLSH
           }
        });
        }
    }

    function GetZPItemsByID(LWLSH)
    {
        var vUrlRead = $api.getStorage('ServerUrlRead');
        api.ajax({
            url:vUrlRead + '/WcfServices/GetWJZPItems_PF/'+LWLSH,
            method: 'post',
            contentType: "application/json",
        }, function(ret, err) {
            if (ret.IsSuccess) {
                var vData = InitZPItemsList(ret.Data);
                ShowList(vData);
            }
        });
    }

    function InitZPItemsList(data)
    {
        if(data.length != 0)
        {
            var vData = new Array([data.length]);
            for (i = 0; i < data.length; i++) {
                var vSLT = data[i].ZP_SLT;
                if(vSLT.length == 0 && vSLT == "")
                {
                    vSLT = 'widget://res/img/Camera.png';
                }

                vData[i] = {
                    uid: data[i].ID,
                    title: data[i].ZP_NAME,
                    imgPath: vSLT,
                    remark: data[i].ZPZT_MS
                }
            }

            return vData;
        }
        else
        {
            return null;
        }
    }

    function ShowList(vData)
    {
        var vHeight = $("#divLXArea").height();
        var vGWBH = $("#selectLineNum").val();
        var vListHeight =api.frameHeight - vHeight;
        UIListView.open({
                rect: {
                    x: 0,
                    y: vHeight,
                    w: api.winWidth,
                    h: vListHeight
                },
                data: vData,
                rightBtns: [
                {
                    bgColor: '#388e8e',
                    activeBgColor: '',
                    width: 70,
                    title: '删除',
                    titleSize: 12,
                    titleColor: '#fff',
                    icon: '',
                    iconWidth: 20
                }],
                styles: {
                    borderColor: '#696969',
                    item: {
                        bgColor: '#AFEEEE',
                        activeBgColor: '#F5F5F5',
                        height: 55.0,
                        imgWidth: 40,
                        imgHeight: 40,
                        imgCorner: 4,
                        placeholderImg: '',
                        titleSize: 16.0,
                        titleColor: '#000',
                        subTitleSize: 12.0,
                        subTitleColor: '#000',
                        remarkColor: '#FF0000',
                        remarkSize: 12,
                        remarkIconWidth: 30
                    }
                },
                fixedOn: api.frameName
            }, function(ret, err) {
                if (ret.eventType == "clickContent") {
                    openCarema(vData[ret.index],api.pageParam.LWLSH,vGWBH,ret.index);
                }
                if(ret.eventType == "clickRightBtn"){
                    //if(ret.btnIndex == 0)
                    //{//删除照片
                    //    DeletePhotoItem(vData[ret.index].uid,api.pageParam.ID,ret.index);
                    //}
                    //else if(ret.btnIndex == 1)
                    //{//选择相册照片
                    //   OpenLocalAlbum(vData[ret.index],api.pageParam.LWLSH,vGWBH,ret.index);
                    //}
                    //else if(ret.btnIndex == 2)
                    //{//查看照片
                    //    ViewPhotoInfo(vData[ret.index].uid,api.pageParam.ID,ret.index);
                    //}
                }

                 UIListView.setRefreshHeader({
                    bgColor: '#F5F5F5',
                    textColor: '#8E8E8E',
                    textDown: '下拉可以刷新...',
                    textUp: '松开开始刷新...',
                    showTime: true
                }, function(ret, err) {
                    if (ret) {
                         GetZPItemsByID(api.pageParam.LWLSH);
                    } else {
                    }
                });
            });
    }

    //删除照片项目
    function DeletePhotoItem(itemId,VehicleID,listId)
    {
        api.confirm({
        title: '提示',
        msg: '您确定要删除吗？',
        buttons: ['确定', '取消']
        }, function(ret, err) {
            if( ret.buttonIndex == 1)
            {
                DeleteZPFromList(itemId);
            }

        });
    }

    //删除照片
    function DeleteZPFromList(itemId)
    {
        var vUrlWrite = $api.getStorage('ServerUrlWrite');

        api.ajax({
            url: vUrlWrite + '/WcfServices/DeleteZPFromList_PF/'+itemId,
            method: 'post',
            contentType: "application/json",
            }, function(ret, err) {
                if (ret.IsSuccess) {
                 GetZPItemsByID(api.pageParam.ID);
            }
            else
            {
                api.alert({
                    title: '错误',
                    msg: ret.Message,
                }, function(ret, err){
                });
            }
        });
    }

    //打开本地相册
    function OpenLocalAlbum(item,lwlsh,gwbh,listId)
    {
         api.getPicture({
            sourceType: 'album',
            encodingType: 'png',
            mediaValue: 'pic',
            destinationType: 'url',
            allowEdit: true,
            quality: 60,
            saveToPhotoAlbum: false
            }, function(ret, err) {
            if (ret) {
                SaveImageToServer(item,lwlsh,gwbh,ret.data,listId);
            } else {
                alert(JSON.stringify(err));
            }
        }); 
    }

    function ViewPhotoInfo(itemId,VehicleID,listId)
    {
        var vUrlRead = $api.getStorage('ServerUrlRead');
        api.ajax({
            url:vUrlRead + '/WcfServices/GetZPCCXXInfoById_PF/'+itemId,
            method: 'post',
            contentType: "application/json",
        }, function(ret, err) {
            if (ret.IsSuccess) 
            {
                var vSLTData =InitSLTItemsList(ret.Data);
                photoBrowser.open({
                images: vSLTData,
                activeIndex:listId,
                placeholderImg: '',
                bgColor: '#000'
                }, function(ret, err) {
                    if (ret) {
                        if(ret.eventType == "click")
                        {
                            photoBrowser.close();                        
                        }
                    } else {
                        alert(JSON.stringify(err));
                    }
                });
            }
        });
    }

    function InitSLTItemsList(data)
    {
        if(data.length != 0)
        {
            var vData = new Array([data.length]);
            for (i = 0; i < data.length; i++) {
                var vSLT = data[i].ZP_SLT;
                if(vSLT.length == 0 && vSLT == "")
                {
                    vSLT = 'widget://res/img/Camera.png';
                }
                vData[i] = vSLT
            }

            return vData;
        }
        else
        {
            return null;
        }        
    }

    function openCarema(item,lwlsh,gwbh,listId)
    {
        api.getPicture({
            sourceType: 'camera',
            encodingType: 'png',
            mediaValue: 'pic',
            destinationType: 'url',
            allowEdit: true,
            quality: 60,
            saveToPhotoAlbum: true
            }, function(ret, err) {
            if (ret) {
                //ShowZPFromAlbum(item,ret.data,listId);
                SaveImageToServer(item,lwlsh,gwbh,ret.data,listId);
            } else {
                alert(JSON.stringify(err));
            }
        }); 
    }


    function ShowZPFromAlbum(itemData,imgPath,listId)
    {
        if(imgPath != "")
        {
            var vRemark =  "正在上传";
            UIListView.updateItem({
            index: listId,
            data: {
                imgPath: imgPath,
                title: itemData.title,
                remark: vRemark
                }
            },
            function(ret, err) {
                if (ret) {
                    UpdateZPBtn(listId);
                } else {
                    alert(JSON.stringify(err));
                }
            });
        }
    }

    function SaveImageToServer(item,lwlsh,gwbh,imgInfo,listId)
    {
        var vUrlWrite = $api.getStorage('ServerUrlWrite');

        api.ajax({
            url: vUrlWrite + '/WcfServices/SaveWJPDAImage_PF/'+lwlsh +'/'+item.uid + '/' + gwbh,
            method: 'post',
            headers: {
                "X-APICloud-AppId": "****",
                },
            data: {    
                files: {
                    "file": imgInfo
                    }
                }
            }, function(ret, err) {
            if (ret.IsSuccess) {
                UpdateZPItem(item,ret.Data,listId);
            }
        });
    }

    function UpdateZPItem(item, ItemData,listId)
    {
        item.imgPath = ItemData.ZP_SLT;
        item.remark = ItemData.ZPZT_MS;
        UIListView.updateItem({
        index: listId,
        data: {
                imgPath: ItemData.ZP_SLT,
                title: ItemData.ZP_NAME,
                remark: ItemData.ZPZT_MS
            }
        },
        function(ret, err) {
            if (ret) {
                UpdateZPBtn(listId);
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    function UpdateZPBtn(listId)
    {
            UIListView.setSwipeBtns({
            index: listId,
            btns: [
            {
                bgColor: '#388e8e',
                activeBgColor: '',
                width: 70,
                title: '删除',
                titleSize: 12,
                titleColor: '#fff',
                icon: '',
                iconWidth: 20
            }]
        }, function(ret, err) {
            if (ret) {

            } else {
                alert(JSON.stringify(err));
            }
        });
    }
</script>